﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.Data;
using Sgr.EntityFrameworkCore.EntityConfigurations;
using Sgr.UPMS.Domain.Users;

namespace Sgr.UPMS.Infrastructure.EntityConfigurations
{
    internal class UserRoleEntityTypeConfiguration : EntityTypeConfigurationBase<UserRole, string>
    {
        public UserRoleEntityTypeConfiguration(DatabaseType? databaseType = null) : base(databaseType)
        { }

        public override void Configure(EntityTypeBuilder<UserRole> builder)
        {
            builder.ToTable("upms_user_role");

            base.Configure(builder);

            builder.Property(f => f.Id).ValueGeneratedOnAdd();

            builder.PropertyAndHasColumnName(b => b.RoleId, GetColumnNameCase())
                .IsRequired()
                .HasComment("角色标识");

            builder.PropertyAndHasColumnName<long>("UserId")
                .IsRequired()
                .HasComment("用户标识");

            // 联合唯一索引，防止重复
            builder.HasIndex("UserId", "RoleId").IsUnique();

            //// 单列索引（可选，根据实际查询量决定是否保留）
            //builder.HasIndex(b => b.RoleId);
            //builder.HasIndex("UserId");
        }
    }
}